//
//  Simple example of how to read from and write to mplayer.
//  Using the -slave option allows you to control playback.
//  In this example, I'm using it to loop a particular section.
//  Works on Windows & Linux,   mac???
//

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.io.*;
import java.util.*;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;

import peer.player2.JavaAccess;

public class MP_Starter {
	
	String cmdline = new String("D:/tmp/SMPlayer/mplayer/mplayer.exe");	
	String flags1 = new String("-input");
	String flags2 = new String("conf=input.conf");
	String flags3 = new String("-quiet");
	String flags4 = new String("-slave");
	// setting following flag to "0" keeps position indicator
	// from coming up at every loop
	String flags5 = new String("-osdlevel");   
	String file = new String("d:/tmp/us.avi");
	JFrame frame = new JFrame() ;
	
	Process process;
	InputStream is;
	OutputStream os;
	InputStreamReader isr;
	OutputStreamWriter osw;
	BufferedReader br;
	BufferedWriter bw;
	PrintWriter pw;
	
	public static void main(String args[]) throws IOException {
		MP_Starter mps = new MP_Starter();
		mps.doStuff();	
	}
	
	public void doStuff() {
		try {
			
			Canvas f=new Canvas();
			f.setVisible(true);
			f.setSize(400,400);
			f.setBackground(new Color(0x1,0x1,0x1));
			JLayeredPane p=new JLayeredPane();p.add(f,JLayeredPane.FRAME_CONTENT_LAYER,-40000);
			frame.getContentPane().add(p,BorderLayout.CENTER);
			//frame.getLayeredPane().add(f,JLayeredPane.FRAME_CONTENT_LAYER);
			frame.setLocation(100, 100);
			frame.setSize(400, 400);
			frame.setVisible(true);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
			long hwnd=JavaAccess.getHWND(f,System.getProperty("sun.boot.library.path")+"\\jawt.dll");
			
			process = new ProcessBuilder(cmdline, flags3, flags4, flags5, "0", "-wid",
					String.valueOf(hwnd), file).start();
			is = process.getInputStream();
			os = process.getOutputStream();
			isr = new InputStreamReader(is);
			osw = new OutputStreamWriter(os);
			br = new BufferedReader(isr);
			bw = new BufferedWriter(osw);
			pw = new PrintWriter(bw);
			// 30.3 seconds start point, 32.7 seconds loop point	
			//Looper l = new Looper(30.3f, 32.7f, pw);  
			//l.begin();
		
			pw.print("gui_play\n");
			pw.flush();  // you have to do this or it won't really get sent out!
			
			String line;
			System.out.println("Output is:"); 
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		}
		catch(Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}	
		
	}
}



///////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/////////////////////////////////////////////////////








